﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace EFDemo_1
{
    class Program
    {
        static void Main(string[] args)
        {
            //CreatePeople();
            //QueryPeople();
            UpdatePeople();
            //DeletePeople();
        }

        private static void CreatePeople()
        {
            using (var context = new EFDemoModel())
            {
                var p = new Person { Name = "Joe Smith" };
                p.Addresses.Add(new Address { City = "Rochester", State = "IL", Street = "124 Juniper", Zip = "62725" });
                p.Phones.Add(new Phone { Number = "2174594484" });
                context.People.Add(p);

                p = new Person { Name = "Sally Jones" };
                p.Addresses.Add(new Address { City = "Springfield", State = "IL", Street = "123 Main", Zip = "62704" });
                p.Phones.Add(new Phone { Number = "2175468794" });
                context.People.Add(p);

                p = new Person { Name = "Billy Anderson" };
                p.Addresses.Add(new Address { City = "Springfield", State = "IL", Street = "1245 Chatham Rd", Zip = "62707" });
                p.Phones.Add(new Phone { Number = "2175665544" });
                context.People.Add(p);

                p = new Person { Name = "Mindy Pierce" };
                p.Addresses.Add(new Address { City = "Springfield", State = "IL", Street = "1460 S. 6th", Zip = "62704" });
                p.Phones.Add(new Phone { Number = "2178148877" });
                context.People.Add(p);

                context.SaveChanges();
            }
        }

        private static void QueryPeople()
        {
            using (var context = new EFDemoModel())
            {
                var NamesWithJoe = context.People.Where(p => p.Name.Contains("Joe")).ToList();

                int CountFromRochester = context.People.Count(p => p.Addresses.Any(a => a.City == "Rochester"));
                
                var FromSpringfield = context.People.Where(p => p.Addresses.Any(a => a.City == "Springfield")).ToList();

                var GroupedByZip = context.People.GroupBy(
                    p => p.Addresses.FirstOrDefault().Zip, 
                    p => new { p.Name, p.Phones.FirstOrDefault().Number })
                    .ToDictionary(g => g.Key, g => g);
            }
        }

        
        private static void UpdatePeople()
        {
            using (var context = new EFDemoModel())
            {
                var from04 = context.People.Where(p => p.Addresses.FirstOrDefault().Zip == "62704");

                foreach (var person in from04)
                {
                    person.Addresses.First().Zip = "62707";
                }

                context.SaveChanges();
            }
        }

        private static void DeletePeople()
        {
            using (var context = new EFDemoModel())
            {
                foreach (var toDelete in context.People)
                {
                    context.People.Remove(toDelete);
                }

                context.SaveChanges();
            }
        }
    }
}
